<div class="container-xl">
  <!-- Page title -->
  <div class="page-header d-print-none">
    <div class="row align-items-center">
      <div class="col">
        <h2 class="page-title">
          站点
        </h2>
      </div>
      <!-- Page title actions -->
      <div class="col-auto ms-auto d-print-none">
        <div class="btn-list">
          <a href="javascript:show_site_modal()" class="btn btn-primary d-none d-sm-inline-block">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
              stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
              <path stroke="none" d="M0 0h24v24H0z" fill="none" />
              <line x1="12" y1="5" x2="12" y2="19" />
              <line x1="5" y1="12" x2="19" y2="12" />
            </svg>
            新增站点
          </a>
          <a href="javascript:show_site_modal()" class="btn btn-primary d-sm-none btn-icon">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
              stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
              <path stroke="none" d="M0 0h24v24H0z" fill="none" />
              <line x1="12" y1="5" x2="12" y2="19" />
              <line x1="5" y1="12" x2="19" y2="12" />
            </svg>
          </a>
          <a href="javascript:show_sitetest_modal()" class="btn d-none d-sm-inline-block">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-activity-heartbeat" width="24"
              height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
              stroke-linejoin="round">
              <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
              <path d="M3 12h4.5l1.5 -6l4 12l2 -9l1.5 3h4.5"></path>
            </svg>
            站点测试
          </a>
          <a href="javascript:show_sitetest_modal()" class="btn d-sm-none btn-icon">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-activity-heartbeat" width="24"
              height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
              stroke-linejoin="round">
              <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
              <path d="M3 12h4.5l1.5 -6l4 12l2 -9l1.5 3h4.5"></path>
            </svg>
          </a>
        </div>
      </div>
    </div>
  </div>
</div>
{% if Sites %}
<div class="page-body">
  <div class="container-xl">
    <div class="row row-cards">
      {% for Site in Sites %}
      <div class="col-12 col-md-6 col-xl-4">
        <div class="card card-link-pop">
          <div class="card-header">
            <h3 class="card-title">
              <span class="badge badge-pill bg-teal"><small>{{ Site.pri }}</small></span>
              <a href="{% if Site.signurl %}{{ Site.signurl }}{% else %}javascript:void(0){% endif %}" {% if Site.signurl
                %}target="_blank" {% endif %}><strong>{{ Site.name }}</strong></a>
            </h3>
            <div class="card-actions btn-actions">
              <a href="javascript:edit_site('{{ Site.id }}')" class="btn-action" title="编辑站点">
                <svg xmlns="http://www.w3.org/2000/svg" class="icon ms-1" width="24" height="24" viewBox="0 0 24 24"
                  stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                  <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                  <path d="M9 7h-3a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-3" />
                  <path d="M9 15h3l8.5 -8.5a1.5 1.5 0 0 0 -3 -3l-8.5 8.5v3" />
                  <line x1="16" y1="5" x2="19" y2="8" />
                </svg>
              </a>
              <a href="javascript:show_site_cookie_ua_modal('{{ Site.id }}', '{{ Site.name }}')" class="btn-action" title="更新站点Cookie和User-Agent">
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-refresh-dot" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                   <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                   <path d="M20 11a8.1 8.1 0 0 0 -15.5 -2m-.5 -4v4h4"></path>
                   <path d="M4 13a8.1 8.1 0 0 0 15.5 2m.5 4v-4h-4"></path>
                   <circle cx="12" cy="12" r="1"></circle>
                </svg>
              </a>
              <a href="javascript:remove_site('{{ Site.name }}', '{{ Site.id }}')" class="btn-action" title="删除站点">
                <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
                  stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                  <desc>Download more icon variants from https://tabler-icons.io/i/x</desc>
                  <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                  <line x1="18" y1="6" x2="6" y2="18" />
                  <line x1="6" y1="6" x2="18" y2="18" />
                </svg>
              </a>
            </div>
          </div>
          <div class="card-body">
            <dl class="row">
              {% if Site.signurl %}
              <dt class="col-3">站点地址:</dt>
              <dd class="col-9">{{ Site.signurl }}</dd>
              {% endif %}
              {% if Site.cookie %}
              <dt class="col-3">COOKIE:</dt>
              <dd class="col-9">***</dd>
              {% endif %}
              {% if Site.rssurl %}
              <dt class="col-3">RSS地址:</dt>
              <dd class="col-9">***</dd>
              {% endif %}
              {% if Site.rule %}
              <dt class="col-3">过滤规则:</dt>
              <dd class="col-9"><span class="badge bg-orange me-2">{{ RuleGroups[Site.rule|string] }}</span></dd>
              {% endif %}
            </dl>
            <div class="row">
              <div class="col">
                {% if Site.signin_enable and (Site.signurl or Site.rssurl) %}
                <span class="badge bg-blue-lt mb-1 me-1">签到</span>
                {% endif %}
                {% if Site.rss_enable and Site.rssurl %}
                <span class="badge bg-azure-lt mb-1 me-1">订阅</span>
                {% endif %}
                {% if Site.brush_enable and Site.rssurl %}
                <span class="badge bg-indigo-lt mb-1 me-1">刷流</span>
                {% endif %}
                {% if Site.statistic_enable and (Site.signurl or Site.rssurl) %}
                <span class="badge bg-purple-lt mb-1 me-1">数据统计</span>
                {% endif %}
                {% if Site.parse == 'Y' %}
                <span class="badge bg-pink-lt mb-1 me-1">解析</span>
                {% endif %}
                {% if Site.unread_msg_notify == 'Y' %}
                <span class="badge bg-orange-lt mb-1 me-1">消息</span>
                {% endif %}
                {% if Site.chrome == 'Y' %}
                <span class="badge bg-yellow-lt mb-1 me-1">仿真</span>
                {% endif %}
                {% if Site.proxy == 'Y' %}
                <span class="badge bg-lime-lt mb-1 me-1">代理</span>
                {% endif %}
              </div>
            </div>
          </div>
        </div>
      </div>
      {% endfor %}
    </div>
  </div>
</div>
{% else %}
<div class="page-body">
  <div class="container-xl d-flex flex-column justify-content-center">
    <div class="empty">
      <div class="empty-img"><img src="./static/img/sign_in.svg" height="128" alt="">
      </div>
      <p class="empty-title">没有站点</p>
      <p class="empty-subtitle text-muted">
        没有添加任何站点，请点击”新增站点“按钮。
      </p>
    </div>
  </div>
</div>
{% endif %}
<div class="modal modal-blur fade" id="modal-site" tabindex="-1" role="dialog" aria-hidden="true"
  data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="modal-site-title">新增站点</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-lg-6">
            <div class="mb-3">
              <label class="form-label required">名称</label>
              <input type="hidden" id="site_id" />
              <input type="text" class="form-control" id="site_name" placeholder="自定义站点名称">
            </div>
          </div>
          <div class="col-lg-6">
            <div class="mb-3">
              <label class="form-label">优先级 <span class="form-help" title="多个站点同时命中订阅时选择下载的优先级，数值越小优先级越高"
                  data-bs-toggle="tooltip">?</span></label>
              <select class="form-select" id="site_pri">
              </select>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-lg-6">
            <div class="mb-3">
              <label class="form-label required">站点地址 <span class="form-help"
                  title="站点访问地址，自动签到、站点数据统计、刷流、内置索引器等将使用，有独立签到页面的站点可以配置为签到页面，否则可以配置为首页"
                  data-bs-toggle="tooltip">?</span></label>
              <input type="text" class="form-control" id="site_signurl" value="" placeholder="站点http地址">
            </div>
          </div>
          <div class="col-lg-6">
            <div class="mb-3">
              <label class="form-label required">站点用途 <span class="form-help" title="设置站点使用场景，选中的场景才会使用该站点"
                  data-bs-toggle="tooltip">?</span></label>
              <div class="form-selectgroup">
                <label class="form-selectgroup-item">
                  <input type="checkbox" name="site_uses" value="Q" class="form-selectgroup-input" checked>
                  <span class="form-selectgroup-label">签到</span>
                </label>
                <label class="form-selectgroup-item">
                  <input type="checkbox" name="site_uses" value="D" class="form-selectgroup-input" checked>
                  <span class="form-selectgroup-label">订阅</span>
                </label>
                <label class="form-selectgroup-item">
                  <input type="checkbox" name="site_uses" value="S" class="form-selectgroup-input" checked>
                  <span class="form-selectgroup-label">刷流</span>
                </label>
                <label class="form-selectgroup-item">
                  <input type="checkbox" name="site_uses" value="T" class="form-selectgroup-input" checked>
                  <span class="form-selectgroup-label">数据统计</span>
                </label>
              </div>
            </div>
          </div>
        </div>
        <div class="col-lg-12">
          <div class="mb-3">
            <label class="form-label">COOKIE <span class="form-help"
                title="站点的Cookie，用于站点自动签到、站点数据统计、刷流、内置索引器等，需要在浏览器中F12->网络页抓取" data-bs-toggle="tooltip">?</span></label>
            <textarea class="form-control" id="site_cookie" rows="2" placeholder="站点COOKIE"></textarea>
          </div>
        </div>
        <div class="row">
          <div class="col-lg-12">
            <div class="mb-3">
              <label class="form-label">RSS订阅地址 <span class="form-help" title="站点订阅源URL地址，用于电影、电视剧订阅、刷流，点击站点RSS图标获取"
                  data-bs-toggle="tooltip">?</span></label>
              <input type="text" class="form-control" id="site_rssurl" value="" placeholder="站点RSS订阅URL">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label required">RSS解析种子详情 <span class="form-help"
                  title="选择是时RSS订阅会解析站点种子详情页面获取种子FREE及HR状态，会增加站点访问压力；选择否时无法判断FREE种，无法实现FREE优先规则；刷流不受此限制"
                  data-bs-toggle="tooltip">?</span></label>
              <select class="form-select" id="site_parse">
                <option value="Y" selected>是</option>
                <option value="N">否</option>
              </select>
            </div>
          </div>
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label">过滤规则 <span class="form-help"
                  title="选择该站点使用的过滤规则组，在设置->过滤规则中设置规则，选择了过滤规则后该站点只有符合规则的种子才会被命中下载；仅作用于RSS、内建索引自动搜索，刷流等不受此限制"
                  data-bs-toggle="tooltip">?</span></label>
              <select class="form-select" id="site_rule">
                <option value="" selected>请选择</option>
                {% for Id, Attr in RuleGroups.items() %}
                <option value="{{ Id }}">{{ Attr }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label required">发送站点未读消息通知 <span class="form-help" title="选择是时检测到站点有未读消息会通过消息推送通知"
                  data-bs-toggle="tooltip">?</span></label>
              <select class="form-select" id="site_unread_msg_notify">
                <option value="Y" selected>是</option>
                <option value="N">否</option>
              </select>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label required">开启浏览器仿真 <span class="form-help"
                  title="开启后站点签到、数据统计可以兼容更多站点，但获取数据耗时会大幅增加；需要拉取含浏览器内核的镜像才能使用" data-bs-toggle="tooltip">?</span></label>
              <select class="form-select" id="site_chrome">
                <option value="Y" selected>是</option>
                <option value="N">否</option>
              </select>
            </div>
          </div>
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label required">使用代理服务器 <span class="form-help"
                  title="开启后该站点的访问将使用代理服务器，代理需在基础设置->系统->代理服务器中设置" data-bs-toggle="tooltip">?</span></label>
              <select class="form-select" id="site_proxy">
                <option value="Y" selected>是</option>
                <option value="N">否</option>
              </select>
            </div>
          </div>
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label required">从详情页下载字幕 <span class="form-help"
                  title="开启后将查询该种子详情页是否有提供字幕，如有则会自动下载" data-bs-toggle="tooltip">?</span></label>
              <select class="form-select" id="site_subtitle">
                <option value="Y" selected>是</option>
                <option value="N">否</option>
              </select>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col">
            <div class="mb-3">
              <label class="form-label">User-Agent <span class="form-help"
                  title="站点签到/数据获取/搜索请求时使用的User-Agent，为空则使用基础配置中User-Agent设置" data-bs-toggle="tooltip">?</span></label>
              <input type="text" class="form-control" id="site_ua" value="" placeholder="站点访问User-Agent">
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button id="site_close_btn" class="btn btn-link">
          关闭
        </button>
        <button id="add_or_edit_site_btn" class="btn btn-primary ms-auto">
          新增
        </button>
      </div>
    </div>
  </div>
</div>
<div class="modal modal-blur fade" id="modal-sitetest" tabindex="-1" role="dialog" aria-hidden="true"
  data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">站点连通性测试</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="table-responsive">
        <table class="table table-vcenter card-table">
          <thead>
            <tr>
              <th class="w-1"><input class="form-check-input m-0 align-middle" id="sitetest_check_all_btn"
                  type="checkbox" aria-label="全选"></th>
              <th>站点</th>
              <th>连通性</th>
              <th>耗时</th>
              <th>错误信息</th>
            </tr>
          </thead>
          <tbody>
            {% for Site in Sites %}
            <tr>
              <td class="w-1"><input class="form-check-input m-0 align-middle" id="check_{{ Site.id }}" type="checkbox">
              </td>
              <td><span>{{ Site.name }}</span></td>
              <td id="sitetest_item{{ Site.id }}"></td>
              <td id="sitetest_item{{ Site.id }}_time"></td>
              <td id="sitetest_item{{ Site.id }}_msg"></td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-link me-auto" data-bs-dismiss="modal">取消</button>
        <button id="sitetest_btn" class="btn btn-primary">测试</button>
      </div>
    </div>
  </div>
</div>
<div class="modal modal-blur fade" id="modal-update-site-cookie" tabindex="-1" role="dialog" aria-hidden="true"
     data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="cookieua_modal_title">更新站点</h5>
        <input type="hidden" id="cookieua_siteid">
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-lg">
            <div class="mb-3">
              <label class="form-label required">用户名</label>
              <input type="text" value="" id="cookieua_username" class="form-control" placeholder="站点登录用户名">
            </div>
          </div>
          <div class="col-lg">
            <div class="mb-3">
              <label class="form-label required">密码</label>
              <input type="password" value="" id="cookieua_password" class="form-control" placeholder="登录密码">
            </div>
          </div>
          <div class="col-lg">
            <div class="mb-3">
              <label class="form-label">两步验证</label>
              <input type="text" value="" id="cookieua_two_step_code" class="form-control" placeholder="如有设置必须填写">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-xl-4">
            <div class="mb-3">
              <label class="form-check form-switch">
                <input class="form-check-input" type="checkbox" id="cookieua_ocrflag">
                <span class="form-check-label">自动识别验证码 <span class="form-help"
                                                              title="开启时将自动识别验证码并填入，关闭时将显示验证码图片并要求手动填入"
                                                              data-bs-toggle="tooltip">?</span></span>
              </label>
            </div>
          </div>
        </div>
      </div>
      <div class="progress progress-sm rounded-0" id="sitecookie_process_bar_div">
        <div class="progress-bar progress-bar-animated progress-bar-indeterminate" id="sitecookie_process_bar"
             style="width: 0" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
        </div>
      </div>
      <div class="modal-body" id="cookieua_result_div" style="display: none">
        <div class="row">
          <div class="alert" id="cookieua_result" role="alert">
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button class="btn btn-link link-secondary me-auto" data-bs-dismiss="modal">
          取消
        </button>
        <button onclick="update_site_cookie_ua()" id="sitecookie_action_btn" class="btn btn-primary">开始更新</button>
      </div>
    </div>
  </div>
</div>
<div class="modal modal-blur fade" id="modal-captcha-code" tabindex="-1" role="dialog" aria-hidden="true"
     data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-body">
        <div class="row">
          <label class="form-label required">请在 30秒 内输入验证码</label>
          <div class="row mb-3">
            <div class="col-auto">
              <input type="hidden" id="captcha_key" value="">
              <img id="captcha_code_img" src="" alt="">
            </div>
            <div class="col">
              <input class="form-control" id="captcha_code" placeholder="">
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <a href="javascript:submit_captcha_code()" class="btn btn-primary">提交</a>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
  //打开新增站点框
  function show_site_modal() {
    $("#site_id").val('');
    $("#site_name").val('');
    $("#site_pri").val('1');
    $("#site_rssurl").val('');
    $("#site_signurl").val('');
    $("#site_cookie").val('');
    $("#site_rule").val('');
    $("#site_ua").val('');
    $("#site_chrome").val('N');
    $("#site_proxy").val('N');
    $("#site_parse").val('Y');
    $("#site_subtitle").val('N');
    $("#site_unread_msg_notify").val('Y');
    $("#site_close_btn").text("关闭");
    $("#modal-site-title").text("新增站点");
    $("#add_or_edit_site_btn").text("新增");
    $("#modal-site").modal("show");
  }
  //编辑站点
  function edit_site(tid) {
    $("#site_id").val(tid);
    $("#site_close_btn").text("删除");
    //根据ID查询详细信息
    ajax_post("get_site", { "id": tid }, function (ret) {
      if (ret.site) {
        $("#site_id").val(ret.site.id);
        $("#site_name").val(ret.site.name);
        $("#site_pri").val(ret.site.pri);
        $("#site_rssurl").val(ret.site.rssurl);
        $("#site_signurl").val(ret.site.signurl);
        $("#site_cookie").val(ret.site.cookie);
        $("#site_parse").val(ret.site.parse);
        $("#site_ua").val(ret.site.ua);
        $("#site_chrome").val(ret.site.chrome);
        $("#site_proxy").val(ret.site.proxy);
        $("#site_subtitle").val(ret.site.subtitle);
        $("#site_unread_msg_notify").val(ret.site.unread_msg_notify)
        $("input[name=site_uses]").each(function () {
          $(this).prop("checked", false);
        });
        $("input[name=site_uses]").each(function () {
          if ($(this).val() == "Q" && ret.site.signin_enable) {
            $(this).prop("checked", true);
          }
          if ($(this).val() == "D" && ret.site.rss_enable) {
            $(this).prop("checked", true);
          }
          if ($(this).val() == "S" && ret.site.brush_enable) {
            $(this).prop("checked", true);
          }
          if ($(this).val() == "T" && ret.site.statistic_enable) {
            $(this).prop("checked", true);
          }
        });
        $("#modal-site-title").text("编辑站点");
        $("#add_or_edit_site_btn").text("修改");
        $("#modal-site").modal("show");
      }
      $("#site_rule").val(ret.site.rule);
    });
  }

  //删除站点
  function del_site(tid) {
    ajax_post("del_site", { "id": tid }, function (ret) {
      navmenu('site');
    });
  }

  //新增站点
  function add_or_edit_site() {
    //id
    let site_id = $("#site_id").val();
    //名称
    let site_name = $("#site_name").val();
    if (site_name == "") {
      $("#site_name").addClass("is-invalid");
      return;
    } else {
      $("#site_name").removeClass("is-invalid");
    }
    //优先级
    let site_pri = $("#site_pri").val();
    if (site_pri) {
      $("#site_pri").removeClass("is-invalid");
    } else {
      $("#site_pri").addClass("is-invalid");
      return;
    }
    //签到URL
    let site_signurl = $("#site_signurl").val();
    if (!site_signurl || site_signurl.startsWith('http')) {
      $("#site_signurl").removeClass("is-invalid");
    } else {
      $("#site_signurl").addClass("is-invalid");
      return;
    }
    //Cookie
    let site_cookie = $("#site_cookie").val();
    //RSS URL
    let site_rssurl = $("#site_rssurl").val();
    if (site_rssurl) {
      if (site_rssurl.startsWith('http')) {
        $("#site_rssurl").removeClass("is-invalid");
      } else {
        $("#site_rssurl").addClass("is-invalid");
        return;
      }
    }
    let site_rule = $("#site_rule").val();
    let site_parse = $("#site_parse").val();
    let site_unread_msg_notify = $("#site_unread_msg_notify").val();
    let site_uses = "";
    $("input[name=site_uses]").each(function () {
      if ($(this).prop("checked")) {
        site_uses = site_uses + $(this).val();
      }
    });
    if (site_uses.match(/[DS]/) && !site_rssurl) {
      $("#site_rssurl").addClass("is-invalid");
      return;
    } else {
      $("#site_rssurl").removeClass("is-invalid");
    };
    let ua = $("#site_ua").val();
    let chrome = $("#site_chrome").val();
    let proxy = $("#site_proxy").val();
    let subtitle = $("#site_subtitle").val();
    let data = {
      "site_id": site_id,
      "site_name": site_name,
      "site_pri": site_pri,
      "site_rssurl": site_rssurl,
      "site_signurl": site_signurl,
      "site_cookie": site_cookie,
      "site_include": site_uses,
      "site_note": {
        "rule": site_rule,
        "parse": site_parse,
        "ua": ua,
        "chrome": chrome,
        "proxy": proxy,
        "message": site_unread_msg_notify,
        "subtitle": subtitle
      }
    }

    $("#add_or_edit_site_btn").text("处理中...").attr("disabled", true);
    // 发送请求
    ajax_post("update_site", data, function (ret) {
      $("#add_or_edit_site_btn").attr("disabled", false).text("新增");
      $("#modal-site").modal("hide");
      //刷新页面
      navmenu('site');
    });
  }

  //关闭或者删除
  function close_or_del() {
    $("#modal-site").modal("hide");
    tid = $("#site_id").val();
    if (tid) {
      del_site(tid);
    }
  }

  //绑定事件
  $("#add_or_edit_site_btn").click(function () {
    add_or_edit_site();
  });
  $("#site_close_btn").click(function () {
    close_or_del();
  });

  //删除站点
  function remove_site(name, siteid) {
    show_ask_modal("是否确认删除站点 " + name + "？", function () {
      hide_ask_modal();
      del_site(siteid);
    });
  }

  // 优先级
  for (let i = 1; i <= 50; i++) {
    $("#site_pri").append('<option value="' + i + '">' + i + '</option>');
  }

  //全选事件
  $("#sitetest_check_all_btn").change(function () {
    $("#modal-sitetest input[type=checkbox]").prop("checked", $(this).prop("checked"));
  });

  //显示测试站点
  function show_sitetest_modal() {
    $("#modal-sitetest").modal("show");
  }

  //测试一个站点
  var total_test_sites = 0
  var finished_test_sites = 0
  function test_one_site(siteid) {
    if (!siteid) {
      return;
    }
    $("#sitetest_btn").attr("disabled", true);
    $("#sitetest_btn").text("测试中...");
    total_test_sites = total_test_sites + 1;
    ajax_post("test_site", { id: siteid }, function (ret) {
      finished_test_sites = finished_test_sites + 1;
      if (finished_test_sites >= total_test_sites) {
        $("#sitetest_btn").attr("disabled", false);
        $("#sitetest_btn").text("测试");
        total_test_sites = 0
        finished_test_sites = 0
      }
      if (ret.code == 0) {
        $(`#sitetest_item${siteid}`).html('<span class="badge bg-green me-1 mb-1">是</span>');
        $(`#sitetest_item${siteid}_time`).html(`<span class="text-green">${ret.time} 毫秒</span>`);
        $(`#sitetest_item${siteid}_msg`).html('');
      } else {
        $(`#sitetest_item${siteid}`).html('<span class="badge bg-red me-1 mb-1">否</span>');
        $(`#sitetest_item${siteid}_time`).html(`<span class="text-red">${ret.time} 毫秒</span>`);
        $(`#sitetest_item${siteid}_msg`).html(`<span class="text-muted">${ret.msg}</span>`);
      }
    });
  }

  // 显示更新站点Cookie和UA对话框
  function show_site_cookie_ua_modal(siteid, sitename){
    $("#cookieua_siteid").val(siteid);
    $("#cookieua_modal_title").text("更新站点Cookie - " + sitename);
    $("#cookieua_result_div").hide();
    $("#sitecookie_process_bar").attr("style", "width: 0").attr("aria-valuenow", 0);
    $("#sitecookie_process_bar_div").hide();
    $("#modal-update-site-cookie").modal("show");
  }

  // 当前正在处理的站点
  var refresh_sitecookie_current = "";
  // 更新站点Cookie和UA
  function update_site_cookie_ua(){
    $("#cookieua_result_div").hide();
    $("#sitecookie_process_bar").attr("style", "width: 0").attr("aria-valuenow", 0);
    let siteid = $("#cookieua_siteid").val();
    let username = $("#cookieua_username").val();
    if (!username) {
      $("#cookieua_username").addClass("is-invalid");
      return;
    }else{
      $("#cookieua_username").removeClass("is-invalid");
    }
    let password = $("#cookieua_password").val();
    if (!password) {
      $("#cookieua_password").addClass("is-invalid");
      return;
    }else{
      $("#cookieua_password").removeClass("is-invalid");
    }
    let cookieua_two_step_code = $("#cookieua_two_step_code").val();

    $("#sitecookie_action_btn").text("正在更新...").attr("disabled", true);
    let ocrflag = $("#cookieua_ocrflag").prop("checked");
    // 刷新进度
    refresh_sitecookie_current = siteid;
    refresh_sitecookie_process_flag = true;
    refresh_sitecookie_fail_count = 0;
    refresh_sitecookie_process(siteid);
    // 发送请求
    ajax_post("update_sites_cookie_ua", { siteid: siteid,
                                          username: username,
                                          password: password,
                                          ocrflag: ocrflag,
                                          two_step_code: cookieua_two_step_code
                                          }, function (ret) {
      $("#sitecookie_process_bar").attr("style", "width: 100%").attr("aria-valuenow", 100);
      $("#sitecookie_action_btn").attr("disabled", false).text("开始更新");
      if (ret.code === 0) {
        $("#cookieua_result").removeClass().addClass("alert alert-success").text(ret.messages);
      }else{
        $("#cookieua_result").removeClass().addClass("alert alert-warning").text(ret.messages);
      }
      $("#cookieua_result_div").show();
      refresh_sitecookie_current = "";
    });

  }

  // 刷新进度标志
  var refresh_sitecookie_process_flag = false;
  // 刷新进度失败次数
  var refresh_sitecookie_fail_count = 0;
  // 刷新站点更新进度，显示验证码
  function refresh_sitecookie_process(siteid) {
    if (!refresh_sitecookie_process_flag) {
      return;
    }
    if (siteid != refresh_sitecookie_current) {
      return;
    }
    $("#sitecookie_process_bar_div").show();
    ajax_post("refresh_process", {type: "sitecookie"}, function (ret) {
      if (ret.code === 0 && ret.value <= 100) {
        // 更新进度条
        $("#sitecookie_process_bar").attr("style", "width: " + ret.value + "%").attr("aria-valuenow", ret.value);
        // 判断是否要输入验证码
        if(ret.text.startsWith("data:image")){
          let code_img = ret.text.split("|")[0]
          let code_key = ret.text.split("|")[1]
          show_captcha_code_modal(code_img, code_key, siteid);
        }
        if (ret.value === 100) {
          refresh_sitecookie_fail_count = refresh_sitecookie_fail_count + 1;
        }

      } else {
        refresh_sitecookie_fail_count = refresh_sitecookie_fail_count + 1;
      }
      if (refresh_sitecookie_fail_count < 5) {
        setTimeout("refresh_sitecookie_process("+siteid+")", 1000);
      }
    });
  }

  // 超时隐藏验证码对话框并恢复更新进度
  function sitecookie_captcha_timeout(siteid) {
    $("#modal-captcha-code").modal("hide");
    if (siteid != refresh_sitecookie_current) {
      return;
    }
    // 恢复刷新
    refresh_sitecookie_process_flag = true;
    refresh_sitecookie_fail_count = 0;
    refresh_sitecookie_process(siteid);
    $("#modal-update-site-cookie").modal("show");
  }

  // 显示验证码输入框
  function show_captcha_code_modal(code_img, code_key, siteid) {
    // 关闭上一个框
    $("#modal-update-site-cookie").modal("hide");
    // 暂停获取状态
    refresh_sitecookie_process_flag = false;
    refresh_sitecookie_fail_count = 0;
    // 显示新框
    $("#captcha_key").val(code_key);
    $("#captcha_code_img").attr("src", code_img);
    $("#captcha_code").val("");
    $("#modal-captcha-code").modal("show");
    // 超时关闭
    setTimeout("sitecookie_captcha_timeout("+siteid+")", 30000);
  }

  // 输入验证码
  function submit_captcha_code(){
    let code = $("#captcha_key").val();
    let value = $("#captcha_code").val();
    if (!value) {
      $("#captcha_code").addClass("is-invalid");
      return;
    }else{
      $("#captcha_code").removeClass("is-invalid");
    }
    ajax_post("set_site_captcha_code", {code: code, value: value}, function (ret) {
      if (ret.code === 0) {
        // 重新刷新状态
        refresh_sitecookie_process_flag = true;
        refresh_sitecookie_fail_count = 0;
        setTimeout("refresh_sitecookie_process("+refresh_sitecookie_current+")", 2000);
        // 关闭框
        $("#modal-captcha-code").modal("hide");
        // 显示原框
        $("#modal-update-site-cookie").modal("show");
      }
    });
  }

  // 站点测试按钮
  $("#sitetest_btn").click(function () {
    $("#modal-sitetest input[type=checkbox]").each(function () {
      if ($(this).attr("id").startsWith("check_") && $(this).prop("checked")) {
        let siteid = $(this).attr("id").replace("check_", "");
        test_one_site(siteid);
      }
    });
  });

</script>